const initWebSocket = function (target,id) {
  // WebSocket与普通的请求所用协议有所不同，ws等同于http，wss等同于https
  target.websock = new WebSocket("ws://localhost:8081/express/socket/"+id);
  target.websock.onopen = this.websocketonopen;
  target.websock.onerror = this.websocketonerror;
  // this.websock.onmessage = this.websocketonmessage;
  target.websock.onclose = this.websocketclose;
  // 接收服务端传过来的消息
  target.websock.onmessage = target.onmessage;
}
const websocketonopen = function () {
  console.log("WebSocket连接成功");
}
const websocketonerror = function (e) {
  console.log("WebSocket连接发生错误");
}
// const websocketonmessage = function (e) {
//   console.log(e.data);
// }
const websocketclose = function (e) {
  console.log("connection closed (" + e.code + ")");
}
const getWebsocket = function(str){
  this.websock.send(str);
}
export const socketApi = {
  initWebSocket: initWebSocket,
  websocketonopen: websocketonopen,
  websocketonerror: websocketonerror,
  // websocketonmessage: websocketonmessage,
  websocketclose: websocketclose,
  getWebsocket: getWebsocket,
}
